%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The impact of divorce laws on the equilibrium in the marriage market.
% Ana Reynoso
% April 2024
%
% This file inputs outcomes under mutual consent and unilateral divorce
% regimes and produces:
%
% - Table 3,
% - Table 4,
% - Figure 5,
% - Figure 6, and
% - Figure 7.
%  
% Data: PSID 1968-1992
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------------------ Replication Path -------------------------------%

clc
clear all

%--- Indicate location of Replication folder:
%replication_location = 'C:\update_with_your_path';

%------------------------ Preliminaries ----------------------------------%

%--- Working directory
cd ([replication_location,'\Figures_and_Tables']);

%--- Path for estimates
input_dir = [replication_location,'\Outputs\MCD\'];
load([input_dir, 'outputs_mcd']);
clearvars -except data_f data_m replication_location

%--- Path for functions
addpath(genpath([replication_location,'\Figures_and_Tables\matrix2latex']));

%--- Path for outputs
output_dir = [replication_location,'\Outputs'];

%--- Parameters for figures
x0=0;
y0=0;
width=1800;
height=1200;

%--- Load estimation outputs

load([output_dir, '\UD\UD_effects_fullequilibrium']);
load([output_dir, '\Commitment\UD_effects_Commitment']);
load([output_dir, '\Divorce_settlement\UD_effects_DivorceSettlement']);
female_educ_bymkt = [data_f(1,:); data_f(4,:); data_f(7,:)];
male_educ_bymkt   =  data_m(1:3,:);

%------------------------ Table 3 ----------------------------------------%

%--- Build the table
%-mkt1
ggks_mc_m1 = round(ggks_mc_bymkt(:,:,1),2);
ggks_ud_m1 = round(ggks_ud_bymkt(:,:,1),2);
%-mkt2
ggks_mc_m2 = round(ggks_mc_bymkt(:,:,2),2);
ggks_ud_m2 = round(ggks_ud_bymkt(:,:,2),2);
%-mkt3
ggks_mc_m3 = round(ggks_mc_bymkt(:,:,3),2);
ggks_ud_m3 = round(ggks_ud_bymkt(:,:,3),2);
%-mkt4
ggks_mc_m4 = round(ggks_mc_bymkt(:,:,4),2);
ggks_ud_m4 = round(ggks_ud_bymkt(:,:,4),2);

table_3 = [ggks_mc_m1 ggks_ud_m1 ggks_mc_m2 ggks_ud_m2 ; ...
           ggks_mc_m3 ggks_ud_m3 ggks_mc_m4 ggks_ud_m4];
       
%--- Store
matrix2latex(table_3, 'Output/Table_3.tex',  'alignment', 'c', 'format', '%-6.2f', 'size', 'tiny');


%------------------------ Table 4 ----------------------------------------%

delta_bymkt=[delta_bymkt_mc delta_bymkt_ud delta_bymkt_ud_ds delta_bymkt_ud_c];
matrix2latex(delta_bymkt, 'Output/Table_4.tex',  'alignment', 'c', 'format', '%-6.2f', 'size', 'tiny');

%------------------------ Figure 5 ---------------------------------------%

%--- Fractions
sahw_mc_hs = (sahw_mc(1,:).*mf_f_mc(1,:) + sahw_mc(4,:).*mf_f_mc(6,:) + sahw_mc(7,:).*mf_f_mc(7,:))./male_educ_bymkt(1,:);
sahw_ud_hs = (sahw_ud(1,:).*mf_f_ud(1,:) + sahw_ud(4,:).*mf_f_ud(6,:) + sahw_ud(7,:).*mf_f_ud(7,:))./male_educ_bymkt(1,:);
sahw_ud_ds_hs = (sahw_ud_ds(1,:).*mf_f_ud_ds(1,:) + sahw_ud_ds(4,:).*mf_f_ud_ds(6,:) + sahw_ud_ds(7,:).*mf_f_ud_ds(7,:))./male_educ_bymkt(1,:);
sahw_ud_c_hs = (sahw_ud_c(1,:).*mf_f_ud_c(1,:) + sahw_ud_c(4,:).*mf_f_ud_c(6,:) + sahw_ud_c(7,:).*mf_f_ud_c(7,:))./male_educ_bymkt(1,:);
sahw_mc_sc = (sahw_mc(2,:).*mf_f_mc(2,:) + sahw_mc(5,:).*mf_f_mc(5,:) + sahw_mc(8,:).*mf_f_mc(8,:))./male_educ_bymkt(2,:);
sahw_ud_sc = (sahw_ud(2,:).*mf_f_ud(2,:) + sahw_ud(5,:).*mf_f_ud(5,:) + sahw_ud(8,:).*mf_f_ud(8,:))./male_educ_bymkt(2,:);
sahw_ud_ds_sc = (sahw_ud_ds(2,:).*mf_f_ud_ds(2,:) + sahw_ud_ds(5,:).*mf_f_ud_ds(5,:) + sahw_ud_ds(8,:).*mf_f_ud_ds(8,:))./male_educ_bymkt(2,:);
sahw_ud_c_sc = (sahw_ud_c(2,:).*mf_f_ud_c(2,:) + sahw_ud_c(5,:).*mf_f_ud_c(5,:) + sahw_ud_c(8,:).*mf_f_ud_c(8,:))./male_educ_bymkt(2,:);
sahw_mc_cp = (sahw_mc(3,:).*mf_f_mc(3,:) + sahw_mc(6,:).*mf_f_mc(6,:) + sahw_mc(9,:).*mf_f_mc(9,:))./male_educ_bymkt(3,:);
sahw_ud_cp = (sahw_ud(3,:).*mf_f_ud(3,:) + sahw_ud(6,:).*mf_f_ud(6,:) + sahw_ud(9,:).*mf_f_ud(9,:))./male_educ_bymkt(3,:);
sahw_ud_ds_cp = (sahw_ud_ds(3,:).*mf_f_ud_ds(3,:) + sahw_ud_ds(6,:).*mf_f_ud_ds(6,:) + sahw_ud_ds(9,:).*mf_f_ud_ds(9,:))./male_educ_bymkt(3,:);
sahw_ud_c_cp = (sahw_ud_c(3,:).*mf_f_ud_c(3,:) + sahw_ud_c(6,:).*mf_f_ud_c(6,:) + sahw_ud_c(9,:).*mf_f_ud_c(9,:))./male_educ_bymkt(3,:);

sahw_fraction_m_mc = [sahw_mc_hs;sahw_mc_sc;sahw_mc_cp];
sahw_fraction_m_ud = [sahw_ud_hs;sahw_ud_sc;sahw_ud_cp];
sahw_fraction_m_ud_ds = [sahw_ud_ds_hs;sahw_ud_ds_sc;sahw_ud_ds_cp];
sahw_fraction_m_ud_c = [sahw_ud_c_hs;sahw_ud_c_sc;sahw_ud_c_cp];

msahw_m1 = [sahw_fraction_m_mc(:,1) sahw_fraction_m_ud(:,1) sahw_fraction_m_ud_ds(:,1) sahw_fraction_m_ud_c(:,1)];
msahw_m2 = [sahw_fraction_m_mc(:,2) sahw_fraction_m_ud(:,2) sahw_fraction_m_ud_ds(:,2) sahw_fraction_m_ud_c(:,2)];
msahw_m3 = [sahw_fraction_m_mc(:,3) sahw_fraction_m_ud(:,3) sahw_fraction_m_ud_ds(:,3) sahw_fraction_m_ud_c(:,3)];
msahw_m4 = [sahw_fraction_m_mc(:,4) sahw_fraction_m_ud(:,4) sahw_fraction_m_ud_ds(:,4) sahw_fraction_m_ud_c(:,4)];

msahw_avg = msahw_m1*0.23 + msahw_m2*0.23 + msahw_m3*0.36 + msahw_m4*0.18;

p_ud_avg = round((msahw_avg(:,2)-msahw_avg(:,1))./msahw_avg(:,1)*100,2);
p_ud_ds_avg = round((msahw_avg(:,3)-msahw_avg(:,1))./msahw_avg(:,1)*100,2);
p_ud_c_avg = round((msahw_avg(:,4)-msahw_avg(:,1))./msahw_avg(:,1)*100,2);

%--- Build the figure
name={'hs men';'sc men';'c+ men'};
p1str=num2str(p_ud_avg(1,1)) ;
p2str=num2str(p_ud_avg(2,1)) ;
p3str=num2str(p_ud_avg(3,1)) ;
labels_ud = {p1str, p2str, p3str};
p1str=num2str(p_ud_ds_avg(1,1)) ;
p2str=num2str(p_ud_ds_avg(2,1)) ;
p3str=num2str(p_ud_ds_avg(3,1)) ;
labels_ud_ds = {p1str, p2str, p3str};
p1str=num2str(p_ud_c_avg(1,1)) ;
p2str=num2str(p_ud_c_avg(2,1)) ;
p3str=num2str(p_ud_c_avg(3,1)) ;
labels_ud_c = {p1str, p2str, p3str};

a = bar(msahw_avg, 'grouped');

legend('MCD','UD full model', 'UD with divorce settlement', 'UD without renegotiation', 'Location','northeast','NumColumns', 4,'FontSize',18, 'FontWeight','bold') ;
set(gca,'xticklabel',name,'FontSize',18, 'FontWeight','bold');
text([0.9 1.9 2.9], msahw_avg(:,2)+0.0005, labels_ud, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
text([1.1 2.1 3.1], msahw_avg(:,3)+0.0005, labels_ud_ds, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
text([1.27 2.27 3.27 ], msahw_avg(:,4)+0.0005, labels_ud_c, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
ylim([0 0.28]);
a(1).FaceColor = [0 0 0];
a(2).FaceColor = [0 0.3470 0.5410]; 
a(3).FaceColor = [0.9 0.9 0.9];
a(4).FaceColor = [1 1 1]; 

%--- Store
set(gcf,'position',[x0,y0,width,height])
saveas(gcf, 'Output/Figure_5.png')  
close

%------------------------ Figure 6 ---------------------------------------%

%--- Probabilities
average_divorce_ud_c = mean(divorce_ud_c,2);
average_divorce_ud_ds = mean(divorce_ud_ds,2);
average_divorce_ud = mean(divorce_ud,2);
average_divorce_mc = mean(divorce_mc,2);
ydiv = [average_divorce_mc  average_divorce_ud average_divorce_ud_ds average_divorce_ud_c];
div_f_mc = reshape(ydiv(:,1), 3,3);
div_f_ud = reshape(ydiv(:,2), 3,3);
div_f_ud_ds = reshape(ydiv(:,3), 3,3);
div_f_ud_c = reshape(ydiv(:,4), 3,3);
divf_hs = [div_f_mc(:,1) div_f_ud(:,1) div_f_ud_ds(:,1) div_f_ud_c(:,1)]; 
divf_sc = [div_f_mc(:,2) div_f_ud(:,2) div_f_ud_ds(:,2) div_f_ud_c(:,2)]; 
divf_cp = [div_f_mc(:,3) div_f_ud(:,3) div_f_ud_ds(:,3) div_f_ud_c(:,3)]; 
p_ud=round((div_f_ud-div_f_mc)./div_f_mc*100,2);
p_ud_ds=round((div_f_ud_ds-div_f_mc)./div_f_mc*100,2);
p_ud_c=round((div_f_ud_c-div_f_mc)./div_f_mc*100,2);

%--- Build the figure
t=tiledlayout(1,3);

% hs 
nexttile;
name={'hs men';'sc men';'c+ men'}; 
p1str=num2str(p_ud(1,1)) ;
p2str=num2str(p_ud(2,1)) ;
p3str=num2str(p_ud(3,1)) ;
labels_ud = {p1str, p2str, p3str};
p1str=num2str(p_ud_ds(1,1)) ;
p2str=num2str(p_ud_ds(2,1)) ;
p3str=num2str(p_ud_ds(3,1)) ;
labels_ud_ds = {p1str, p2str, p3str};
p1str=num2str(p_ud_c(1,1)) ;
p2str=num2str(p_ud_c(2,1)) ;
p3str=num2str(p_ud_c(3,1)) ;
labels_ud_c = {p1str, p2str, p3str};

a = bar(divf_hs, 'grouped');

xlabel('hs women')
legend('MCD','UD full model', 'UD with divorce settlement', 'UD without renegotiation', 'Location','northwest','NumColumns', 2,'FontSize',12, 'FontWeight','bold') ;
set(gca,'xticklabel',name,'FontSize',20, 'FontWeight','bold');
text([0.9 1.9 2.9], divf_hs(:,2)+0.002, labels_ud, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.1 2.1 3.1], divf_hs(:,3)+0.002, labels_ud_ds, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.27 2.27 3.27 ], divf_hs(:,4)+0.002, labels_ud_c, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
ylim([0 0.6])
a(1).FaceColor = [0 0 0];
a(2).FaceColor = [0 0.3470 0.5410]; 
a(3).FaceColor = [0.9 0.9 0.9]; 
a(4).FaceColor = [1 1 1]; 

% sc 
nexttile;

p1str=num2str(p_ud(1,2)) ;
p2str=num2str(p_ud(2,2)) ;
p3str=num2str(p_ud(3,2)) ;
labels_ud = {p1str, p2str, p3str};
p1str=num2str(p_ud_ds(1,2)) ;
p2str=num2str(p_ud_ds(2,2)) ;
p3str=num2str(p_ud_ds(3,2)) ;
labels_ud_ds = {p1str, p2str, p3str};
p1str=num2str(p_ud_c(1,2)) ;
p2str=num2str(p_ud_c(2,2)) ;
p3str=num2str(p_ud_c(3,2)) ;
labels_ud_c = {p1str, p2str, p3str};

a = bar(divf_sc, 'grouped');

xlabel('sc women')
set(gca,'xticklabel',name,'FontSize',20, 'FontWeight','bold');
text([0.9 1.9 2.9], divf_sc(:,2)+0.002, labels_ud, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.1 2.1 3.1], divf_sc(:,3)+0.002, labels_ud_ds, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.27 2.27 3.27 ], divf_sc(:,4)+0.002, labels_ud_c, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
ylim([0 0.6])
a(1).FaceColor = [0 0 0];
a(2).FaceColor = [0 0.3470 0.5410]; 
a(3).FaceColor = [0.9 0.9 0.9]; 
a(4).FaceColor = [1 1 1]; 

% c+ 
nexttile;

p1str=num2str(p_ud(1,3)) ;
p2str=num2str(p_ud(2,3)) ;
p3str=num2str(p_ud(3,3)) ;
labels_ud = {p1str, p2str, p3str};
p1str=num2str(p_ud_ds(1,3)) ;
p2str=num2str(p_ud_ds(2,3)) ;
p3str=num2str(p_ud_ds(3,3)) ;
labels_ud_ds = {p1str, p2str, p3str};
p1str=num2str(p_ud_c(1,3));
p2str=num2str(p_ud_c(2,3));
p3str=num2str(p_ud_c(3,3));
labels_ud_c = {p1str, p2str, p3str};

a = bar(divf_cp, 'grouped');

xlabel('c+ women')
set(gca,'xticklabel',name,'FontSize',20, 'FontWeight','bold');
text([0.9 1.9 2.9], divf_cp(:,2)+0.002, labels_ud, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.1 2.1 3.1], divf_cp(:,3)+0.002, labels_ud_ds, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
text([1.27 2.27 3.27 ], divf_cp(:,4)+0.002, labels_ud_c, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',10, 'FontWeight','bold','Rotation',0);
ylim([0 0.6])
a(1).FaceColor = [0 0 0]; 
a(2).FaceColor = [0 0.3470 0.5410];
a(3).FaceColor = [0.9 0.9 0.9]; 
a(4).FaceColor = [1 1 1]; 

%--- Store
set(gcf,'position',[x0,y0,width,height])
saveas(gcf, 'Output/Figure_6.png')  
close

%------------------------ Figure 7 ---------------------------------------%

%--- Gains
gmf_m1 = [GMf_mc(:,1) GMf_ud(:,1) GMf_ud_ds(:,1) GMf_ud_c(:,1)];
gmf_m2 = [GMf_mc(:,2) GMf_ud(:,2) GMf_ud_ds(:,2) GMf_ud_c(:,2)];
gmf_m3 = [GMf_mc(:,3) GMf_ud(:,3) GMf_ud_ds(:,3) GMf_ud_c(:,3)];
gmf_m4 = [GMf_mc(:,4) GMf_ud(:,4) GMf_ud_ds(:,4) GMf_ud_c(:,4)];
p_ud=round(Pi_c_f.*(-1),2);
p_ud_ds=round(Pi_c_f_ds.*(-1),2);
p_ud_c=round(Pi_c_f_c.*(-1),2);
gmf_avg = gmf_m1*0.23 + gmf_m2*0.23 + gmf_m3*0.36 + gmf_m4*0.18;
p_ud_avg = round(p_ud(:,1)*0.23 + p_ud(:,2)*0.23 + p_ud(:,3)*0.36 + p_ud(:,4)*0.18,2);
p_ud_ds_avg = round(p_ud_ds(:,1)*0.23 + p_ud_ds(:,2)*0.23 + p_ud_ds(:,3)*0.36 + p_ud_ds(:,4)*0.18,2);
p_ud_c_avg = round(p_ud_c(:,1)*0.23 + p_ud_c(:,2)*0.23 + p_ud_c(:,3)*0.36 + p_ud_c(:,4)*0.18,2);

%--- Build the figure
name={'hs';'sc';'c+'};
p1str=num2str(p_ud_avg(1,1)) ;
p2str=num2str(p_ud_avg(2,1)) ;
p3str=num2str(p_ud_avg(3,1)) ;
labels_ud = {p1str, p2str, p3str};
p1str=num2str(p_ud_ds_avg(1,1)) ;
p2str=num2str(p_ud_ds_avg(2,1)) ;
p3str=num2str(p_ud_ds_avg(3,1)) ;
labels_ud_ds = {p1str, p2str, p3str};
p1str=num2str(p_ud_c_avg(1,1)) ;
p2str=num2str(p_ud_c_avg(2,1)) ;
p3str=num2str(p_ud_c_avg(3,1)) ;
labels_ud_c = {p1str, p2str, p3str};

a = bar(gmf_avg, 'grouped');

legend('MCD','UD full model', 'UD with divorce settlement', 'UD without renegotiation', 'Location','northeast','NumColumns', 4,'FontSize',20, 'FontWeight','bold') ;
set(gca,'xticklabel',name,'FontSize',26, 'FontWeight','bold');
text([0.9 1.9 2.9], gmf_avg(:,2)+0.002, labels_ud, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
text([1.1 2.1 3.1], gmf_avg(:,3)+0.002, labels_ud_ds, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
text([1.27 2.27 3.27 ], gmf_avg(:,4)+0.002, labels_ud_c, 'HorizontalAlignment','center', 'VerticalAlignment','bottom','FontSize',20, 'FontWeight','bold','Rotation',0);
ylim([0 2.8])
a(1).FaceColor = [0 0 0]; 
a(2).FaceColor = [0 0.3470 0.5410]; 
a(3).FaceColor = [0.9 0.9 0.9]; 
a(4).FaceColor = [1 1 1]; 

%--- Store
set(gcf,'position',[x0,y0,width,height])
saveas(gcf, 'Output/Figure_7.png')  
close

